<?php

/**
 * Module Tree
 * @since      Version 0.1
 * @filesource
 */

/**
 * Description of HYD_Module_Tree
 
 * @author zch
 */
class HYD_Module_Tree {

    private $_ci = null;
    private $_data = array();
    private $_show_global_root = TRUE;

    public function __construct($config = array()) {
        //initialize the ci instance
        $this->_ci = &get_instance();
        $this->initialize($config);
    }

    /**
     * 初始化
     * @param array $config
     */
    private function initialize($config) {
        if (!empty($config)) {
            foreach ($config as $key => $val) {
                $key = '_' . $key;
                if (isset($this->$key)) {
                    $this->$key = $val;
                }
            }
        }
        //加载module模型
        $this->_ci->load->model('module/module_model');
        $module_rows = $this->_ci->module_model->get_select_arr(0);

        $this->_data['module_list'] = array();
        foreach ($module_rows as $row) {
            $this->_data['module_list'][$row['id']] = array(
                'id' => $row['id'],
                'text' => $row['text'],
                'name' => $row['name'],
            );
        }
    }

    /**
     * 生成树
     * 
     * @return array
     */
    public function build_ext_json_tree() {
        $result = array();
        if ($this->_show_global_root == TRUE) {
            
        }
     
        foreach ($this->_data['module_list'] as $id => $con) {
            $children_rows = $this->_ci->module_model->get_select_arr($con['id']);
            if(count($children_rows)>0){
                $data = array();
                foreach ($children_rows as $row) {
                    $data[] = array(
                        'id' => $row['id'],
                        'text' => $row['text'],
                        'tag' => $row['name'],
                        'leaf' => TRUE
                    );
                }
                $result[] = array(
                    'id' => $con['id'],
                    'text' => $con['text'],
                    'tag' => $con['name'],
                    'leaf' => FALSE,
                    'expanded' => TRUE,
                    'children' => $data
                );
            }else{
                $result[] = array(
                    'id' => $con['id'],
                    'text' => $con['text'],
                    'tag' => $con['name'],
                    'leaf' => TRUE,
                    'expanded' => FALSE
                );
            }
        }

        return $result;
    }

}

?>
